//
//  CommentInputView.m
//  工程机械观察
//
//  Created by 毕武侠 on 2016/12/15.
//  Copyright © 2016年 毕武侠. All rights reserved.
//

#import "CommentInputView.h"

@interface CommentInputView ()

@property (nonatomic, strong) UIView *blackView;    // 黑色背景
@property (nonatomic, strong) UIView *inputView;    // 输入框的背景
@property (nonatomic, strong) UITextView *textView; // 输入框
@property (nonatomic, strong) UIButton  *sendBtn;   // 发送按钮

@property (nonatomic, assign) CGFloat keyboardHeight;

@end

@implementation CommentInputView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initConfigureView];
    }
    
    return self;
}

- (void)initConfigureView {
    //
    self.backgroundColor = [UIColor clearColor];
    self.hidden = YES;
    
    // add black view
    self.blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.blackView.backgroundColor = [UIColor blackColor];
    self.blackView.alpha = 0.0;
    UITapGestureRecognizer*tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backAction:)];
    [self.blackView addGestureRecognizer:tapGesture];
    [self addSubview:self.blackView];
    
    // add input View
    self.inputView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 148)];
    self.inputView.backgroundColor = RGB(243, 243, 243);
    [self addSubview:self.inputView];
    
    // add text view
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(16, 16, SCREEN_WIDTH - 32, 80)];
    self.textView.layer.masksToBounds = YES;
    self.textView.font = [UIFont systemFontOfSize:16.0f];
    self.textView.layer.cornerRadius = 4.0f;
    self.textView.backgroundColor = [UIColor whiteColor];
    self.textView.inputAccessoryView = [[UIView alloc] init];
    [self.inputView addSubview:self.textView];
    
    // add send btn
    self.sendBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 76, CGRectGetMaxY(self.textView.frame) + 12, 60, 30)];
    self.sendBtn.layer.masksToBounds = YES;
    self.sendBtn.layer.cornerRadius = 4.0f;
    self.sendBtn.backgroundColor = MainThemeColor;
    [self.sendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.sendBtn setTitle:@"发送" forState:UIControlStateNormal];
    [self.sendBtn addTarget:self action:@selector(sendAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.inputView addSubview:self.sendBtn];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShowFrame:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHiddenFrame:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

#pragma mark - notification handler

- (void)keyboardWillShowFrame:(NSNotification *)notification {
    NSDictionary *info = [notification userInfo];
    CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGRect keyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    
    CGRect rect = self.inputView.frame;
    rect.origin.y = SCREEN_HEIGHT - keyboardRect.size.height - 148;
    if (self.keyboardHeight == 0) {
        self.keyboardHeight = keyboardRect.size.height;
    }

    [UIView animateWithDuration:duration + 0.1 animations:^{
        self.inputView.frame = rect;
    }];
}

- (void)keyboardWillHiddenFrame:(NSNotification *)notification {
    [self hidden];
}

// 显示
- (void)show {
    self.hidden = NO;
    if ([self.textView isFirstResponder] == YES) {
        CGRect rect = self.inputView.frame;
        rect.origin.y = SCREEN_HEIGHT - self.keyboardHeight - 148;
        [UIView animateWithDuration:0.32 animations:^{
            self.inputView.frame = rect;
        }];
    } else {
        [self.textView becomeFirstResponder];
        [UIView animateWithDuration:0.32 animations:^{
            self.blackView.alpha = 0.7f;
        }];
    }
}

// 隐藏
- (void)hidden {
    [self.textView resignFirstResponder];
    [UIView animateWithDuration:0.24 animations:^{
        self.blackView.alpha = 0.0f;
        self.inputView.frame = CGRectMake(0, SCREEN_HEIGHT - 148, SCREEN_WIDTH, 148);
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}

- (void)clearText {
    self.textView.text = nil;
}

// 点击黑背景
- (void)backAction:(id)sender {
    [self hidden];
}

// 发送
- (void)sendAction:(id)sender {
    if(self.textView.text && self.textView.text.length > 0 && [self.delegate respondsToSelector:@selector(commentInputViewDidSend:)]) {
        [self.delegate commentInputViewDidSend:self.textView.text];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
